/*
 * (C) Copyright 2014-2018, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.monotone


import junit.framework.TestCase.assertEquals
import org.jheaps.AddressableHeap
import org.junit.Assert
import org.junit.Test
import java.io.*
import java.util.*
import kotlin.math.abs

class IntegerRadixAddressableHeapTest {
    @Test
    fun testBug2() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 100)
        h.insert(0)
        assertEquals(0, h.findMin().key)
        assertEquals(0, h.deleteMin().key)
        h.insert(15)
        assertEquals(15, h.findMin().key)
    }

    @Test
    fun testVerySmall() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(15, 100)

        h.insert(15)
        h.insert(50)
        h.insert(21)
        h.insert(51)
        h.insert(30)
        h.insert(25)
        h.insert(18)

        Assert.assertEquals(7, h.size())
        assertEquals(15, h.findMin().key)
        Assert.assertEquals(7, h.size())
        assertEquals(15, h.deleteMin().key)
        Assert.assertEquals(6, h.size())
        assertEquals(18, h.findMin().key)
        assertEquals(18, h.deleteMin().key)
        assertEquals(21, h.findMin().key)
        assertEquals(21, h.deleteMin().key)
        assertEquals(25, h.findMin().key)
        assertEquals(25, h.deleteMin().key)
        assertEquals(30, h.findMin().key)
        assertEquals(30, h.deleteMin().key)
        assertEquals(50, h.findMin().key)
        assertEquals(50, h.deleteMin().key)
        assertEquals(51, h.findMin().key)
        assertEquals(51, h.deleteMin().key)
        Assert.assertEquals(h.size(), 0)
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun test() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, SIZE)

        for (i in 0 until SIZE) {
            h.insert(i)
            //assertEquals(0, h.findMin().key, 1e-9)
            Assert.assertFalse(h.isEmpty)
            Assert.assertEquals(h.size(), (i + 1).toLong())
        }

        for (i in SIZE - 1 downTo 0) {
            //assertEquals(SIZE - i - 1, h.findMin().key, 1e-9)
            h.deleteMin()
        }
    }

    @Test
    fun testSortRandomSeed1() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, Int.MAX_VALUE)

        val generator = Random(1)

        h.insert(0)
        for (i in 1 until SIZE) {
            val d = (abs(generator.nextInt().toDouble()) + 1).toInt()
            h.insert(d)
        }

        var prev: Int? = null
        var cur: Int
        while (!h.isEmpty) {
            cur = h.deleteMin().key
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSortRandomSeed2() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, Int.MAX_VALUE)

        val generator = Random(2)

        h.insert(0)
        for (i in 1 until SIZE) {
            val d = (abs(generator.nextInt().toDouble()) + 1).toInt()
            h.insert(d)
        }

        var prev: Int? = null
        var cur: Int
        while (!h.isEmpty) {
            cur = h.deleteMin().key
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSameMinMax() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(1, 1)

        for (i in 0..14) {
            h.insert(1)
        }

        Assert.assertEquals(15, h.size())
        for (i in 0..14) {
            assertEquals(1, h.deleteMin().key)
        }
        Assert.assertEquals(0, h.size())
    }

    @Test
    fun testMaxDifference() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, Int.MAX_VALUE)

        h.insert(0)
        h.insert(Int.MAX_VALUE)

        Assert.assertEquals(2, h.size())
        assertEquals(0, h.deleteMin().key)
        assertEquals(Int.MAX_VALUE, h.deleteMin().key)
        Assert.assertEquals(0, h.size())
    }

    @Test
    fun testDelete() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 15)

        val array: Array<AddressableHeap.Handle<Int, Void>> = Array(15){i->h.insert(i)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i)
        }*/

        array[5].delete()
        assertEquals(0, h.findMin().key)
        array[7].delete()
        assertEquals(0, h.findMin().key)
        array[0].delete()
        assertEquals(1, h.findMin().key)
        array[2].delete()
        assertEquals(1, h.findMin().key)
        array[1].delete()
        assertEquals(3, h.findMin().key)
        array[3].delete()
        assertEquals(4, h.findMin().key)
        array[9].delete()
        assertEquals(4, h.findMin().key)
        array[4].delete()
        assertEquals(6, h.findMin().key)
        array[8].delete()
        assertEquals(6, h.findMin().key)
        array[11].delete()
        assertEquals(6, h.findMin().key)
        array[6].delete()
        assertEquals(10, h.findMin().key)
        array[12].delete()
        assertEquals(10, h.findMin().key)
        array[10].delete()
        assertEquals(13, h.findMin().key)
        array[13].delete()
        assertEquals(14, h.findMin().key)
        array[14].delete()
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testDelete1() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 10)

        val array: Array<AddressableHeap.Handle<Int, Void>> = Array(8){i->h.insert(i)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(8)
        for (i in 0..7) {
            array[i] = h.insert(i)
        }*/

        /*array[5].delete()
        assertEquals(0, h.findMin().key, 1e-9)
        array[7].delete()
        assertEquals(0, h.findMin().key, 1e-9)
        array[0].delete()
        assertEquals(1, h.findMin().key, 1e-9)
        array[2].delete()
        assertEquals(1, h.findMin().key, 1e-9)
        array[1].delete()
        assertEquals(3, h.findMin().key, 1e-9)*/
    }

    @Test
    fun testAddDelete() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, SIZE)

        val array: Array<AddressableHeap.Handle<Int, Void>>  = Array(SIZE){ i->h.insert(i)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(SIZE)
        for (i in 0 until SIZE) {
            array[i] = h.insert(i)
        }*/

        for (i in SIZE - 1 downTo 0) {
            array[i].delete()
            if (i > 0) {
                assertEquals(0, h.findMin().key)
            }
        }
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testClear() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 15)

        for (i in 0..14) {
            h.insert(i)
        }

        h.clear()
        Assert.assertEquals(0L, h.size())
        Assert.assertTrue(h.isEmpty)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteTwice() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 15)

        val array: Array<AddressableHeap.Handle<Int, Void>> = Array(15){i->h.insert(i)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i)
        }*/

        array[5].delete()
        assertEquals(0, h.findMin().key)
        array[7].delete()
        assertEquals(0, h.findMin().key)
        array[0].delete()
        assertEquals(1, h.findMin().key)
        array[2].delete()
        assertEquals(1, h.findMin().key)
        array[1].delete()
        assertEquals(3, h.findMin().key)
        array[3].delete()
        assertEquals(4, h.findMin().key)
        array[9].delete()
        assertEquals(4, h.findMin().key)
        array[4].delete()
        assertEquals(6, h.findMin().key)

        // again
        array[2].delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMinDeleteTwice() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 100)
        val e1 = h.insert(50)
        h.insert(100)
        h.deleteMin()
        e1.delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMinDeleteTwice1() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(99, 200)

        for (i in 100..199) {
            h.insert(i)
        }

        h.deleteMin().delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMinDecreaseKey() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(100, 200)

        for (i in 100..199) {
            h.insert(i)
        }
        h.deleteMin().decreaseKey(0)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteEmpty() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 200)
        val handle = h.insert(1)
        h.deleteMin()
        handle.delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDecreaseKeyEmpty() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(100, 200)
        val handle = h.insert(150)
        h.deleteMin()
        handle.decreaseKey(120)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDecreaseKeyMore() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(100, 200)
        val handle = h.insert(150)
        handle.decreaseKey(160)
    }

    @Test
    fun testDecreaseKeySame() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(100, 200)
        val handle = h.insert(150)
        handle.decreaseKey(150)
        assertEquals(150, h.findMin().key)
    }

    @Test
    fun testDecreaseKey() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 200)

        h.insert(0) // monotone

        val array: Array<AddressableHeap.Handle<Int, Void>> = Array(15){i->h.insert(i + 100)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)

        for (i in 0..14) {
            array[i] = h.insert(i + 100)
        }*/

        array[5].decreaseKey(5)
        array[1].decreaseKey(50)
        array[10].decreaseKey(3)
        array[0].decreaseKey(1)
        array[5].delete()
        array[2].delete()
        array[11].delete()
        array[9].delete()

        assertEquals(0, h.deleteMin().key)
        assertEquals(1, h.deleteMin().key)
        assertEquals(3, h.deleteMin().key)
        assertEquals(50, h.deleteMin().key)
        assertEquals(103, h.deleteMin().key)
        assertEquals(104, h.deleteMin().key)

        array[14].decreaseKey(111)
        array[13].decreaseKey(109)

        assertEquals(106, h.deleteMin().key)
        assertEquals(107, h.deleteMin().key)
        assertEquals(108, h.deleteMin().key)
        assertEquals(109, h.deleteMin().key)
        assertEquals(111, h.deleteMin().key)
        assertEquals(112, h.deleteMin().key)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIncreaseKey() {
        val h: AddressableHeap<Int, Void> = IntegerRadixAddressableHeap(0, 200)

        val array: Array<AddressableHeap.Handle<Int, Void>> = Array(15){i->h.insert(i + 100)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i + 100)
        }*/

        assertEquals(100, h.findMin().key)
        array[5].decreaseKey(5)
        assertEquals(5, h.findMin().key)
        array[1].decreaseKey(102)
    }

    @Test
    @Throws(IOException::class, ClassNotFoundException::class)
    fun testSerializable() {
        var h = IntegerRadixAddressableHeap<Void>(0, 15)

        for (i in 0..14) {
            h.insert(i)
        }

        // write
        val baos = ByteArrayOutputStream()
        val oos = ObjectOutputStream(baos)
        oos.writeObject(h)
        oos.close()
        val data = baos.toByteArray()

        // read
        val ois = ObjectInputStream(ByteArrayInputStream(data))
        val o = ois.readObject()
        ois.close()
        h = o as IntegerRadixAddressableHeap<Void>

        for (i in 0..14) {
            Assert.assertEquals((15 - i).toLong(), h.size())
            assertEquals(i, h.findMin().key)
            h.deleteMin()
        }
        Assert.assertTrue(h.isEmpty)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction() {
        IntegerRadixAddressableHeap<Void>(-1, 10)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction1() {
        IntegerRadixAddressableHeap<Void>(10, 9)
    }

    companion object {
        private const val SIZE = 100000
    }
}
